﻿#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<string.h>
//qosrt函数排序各种类型数据
排列整型
int compare(int* arr,int* arr1)
{
	return(*(int*)arr - *(int*)arr1);
}
int main()
{
	int arr[] = { 1,3,5,2,8,6,4,9,7 };
	qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof(arr[0]), compare);
	for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}




//排列字符串
int compare(char* arr, char* arr1)
{
	return(*(char*)arr - *(char*)arr1);
}
int main()
{
	char arr[] = { "ACBFDE"};
	qsort(arr, strlen(arr), sizeof(arr[0]), compare);
	for (int i = 0; i < strlen(arr); i++)
	{
		printf("%c ", arr[i]);
	}
	return 0;
}


//排列浮点数
int compare(double* arr,double* arr1)
{
	if (*(double*)arr > *(double*)arr1)
		return 1;
	else if(*(double*)arr == *(double*)arr1)
		return 0;
	else
		return -1;
}
int main()
{
	double arr[] = { 1.3,3.2,5.1,2.5,8.4,6.9,4.7,9.1,7.5 };
	qsort(arr, sizeof(arr) / sizeof(arr[0]), sizeof(arr[0]), compare);
	for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		printf("%f ", arr[i]);
	}
	return 0;
}




//模拟实现qsort
int compare(const void* arr,const void* arr1)
{
	return(*(int*)arr - *(int*)arr1);
}
void swap(void* p1, void* p2, int size)
{
	for (int i = 0; i < size; i++)
	{
		char tmp = *((char*)p1+i);
		*((char*)p1+i) = *((char*)p2+i);
		*((char*)p2+i) = tmp;
	}
}
 void My_qsort(void* arr, int sz,int size,int (*compare)(void*,void*))
{
	for (int i = 0; i < sz; i++)
	{
		for (int j = 0; j < sz - i - 1; j++)
		{
			if (compare((char*)arr + j * size, (char*)arr + (j + 1) * size) > 0)
			{
				swap((char*)arr + j * size, (char*)arr + (j + 1) * size, size);
			}
		}
	}
}
int main()
{
	int arr[] = { 1,3,2,6,4,9,5,7,8 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	My_qsort(arr, sz, sizeof(arr[0]),compare);
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}